package com.lcredu.aop.aspectj;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author 黎长容
 * @date 2025-07-15 15:27
 * 这是一个切面类
 */
@Order(value = 1)
@Aspect
@Component
public class SmartAnimalAspect3 {

    //写一个前置通知,作用于public int getSum(int num1, int num2)
    @Before(value = "execution(public int com.lcredu.aop.aspectj.SmartDog.getSum(int, int))")
    public void before(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect3---before--前置通知---日志-方法名-" + signature.getName() + "-参数" + Arrays.asList(joinPoint.getArgs()));
    }

    //后置通知
    //后置通知拿到返回结果可以添加属性returning
    //细节: returning = "res" 里面的res要和Object res里的res一致
    @AfterReturning(value = "execution(public int com.lcredu.aop.aspectj.SmartDog.getSum(int ,int ))", returning = "res")
    public void afterReturn(JoinPoint joinPoint, Object res) {
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect3---after--后置通知--方法名=" + signature.getName() + "返回结果为" + res);
    }

    //异常通知
    //异常通知获取异常信息
    @AfterThrowing(value = "execution(public int com.lcredu.aop.aspectj.SmartDog.getSum(int ,int ))", throwing = "throwable")
    public void yc(JoinPoint joinPoint, Throwable throwable) {
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect3---exception--异常通知--方法名=" + signature.getName() + " 异常信息=" + throwable);
    }

    //最终通知
    @After(value = "execution(public int com.lcredu.aop.aspectj.SmartDog.getSum(int ,int ))")
    public void after(JoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        System.out.println("SmartAnimalAspect3---after--最终通知--方法名=" + signature.getName());
    }
}
